<template>
	<view>
		<view v-if='item != ""' class='group'>
			<view style='padding:10px 0px;'>
				<text style='color:#303133;font-weight: bold;'>单号：{{item.no.slice(-8)}}</text>
				<text style='color:#606266;font-weight: bold;float:right'>{{orderStatus[item.status]}}</text>
			</view>
			<u-line></u-line>
			<view>
				<view v-for='spec in item.items'>
					<view>
						<view style='color:#303133;font-weight: bold;line-height: 40px;'>
							<text
								style='color:#f9ae3d'>{{spec.__metadata.specification.__metadata.product.__toString}}({{spec.__metadata.specification.__metadata.name}})</text>
							<text style='color:#909399;float:right'>￥{{spec.__metadata.unitPrice}} X {{spec.__metadata.quantity}}</text>
						</view>
					</view>
					<u-line></u-line>
				</view>
				<view>
					<view style='color:#303133;font-weight: bold;line-height: 40px;'>
						<text style='color:#f9ae3d'>合计：</text>
						<text style='color:#909399;float:right'>￥{{item.price}}</text>
					</view>
					<u-line></u-line>
				</view>
			</view>

			<view style='padding-top:5px'>
				<view v-if='item.staff'>
					<view>
						<text class='title'>{{item.staff.__toString}}</text>
						<text class='name'>￥{{(parseFloat(item.price)+parseFloat(item.paidCounter)).toFixed(2)}}</text>
					</view>
					<u-line></u-line>
				</view>
				<view>
					<text class='title'>创建时间</text>
					<text class='name'>
						{{$u.timeFormat(new Date(item.createdTime), 'yyyy-mm-dd hh:MM:ss')}}
					</text>
				</view>
				<u-line></u-line>
			</view>
		</view>
		<view class='footer'>
			<view style='padding:10px;'>
				<view v-if='item.status == 2 && canVerify == true'>
					<u-button type='primary' text='确认核销' :plain='true' shape='circle' @click='verify'></u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				item: '',
				orderStatus: {
					'-2': '支付失败',
					'-1': '已取消',
					0: '等待支付',
					1: '待支付',
					2: '待核销',
					3: '已核销',
					4: '已结算',
					5: '已完成'
				},
				canVerify: false
			}
		},
		onShow() {
			
		},
		onLoad(options) {
			if (typeof(options.scene) != 'undefined') {
				this.request.get(`public/orders/${options.scene}`, {
					'@expands': "['entity.items.specification']"
				}).then((res) => {
					console.log(res.data)
					this.item = res.data
					if (res.data.status > 2) {
						uni.showModal({
							title: '',
							content: '订单已核销！',
							showCancel: false,
							success: () => {

							}
						})
					}else{
						let api_token = uni.getStorageSync('api_token')
						if(api_token == ''){
							uni.showModal({
								title: '',
								content: '请先登录',
								showCancel: false,
								success: () => {
									uni.navigateTo({
										url: '/pages/index/login'
									})
								}
							})
						}else{
							this.request.get(`api/user`).then((res) => {
								console.log('user',res.data)
								if(res.data.roles.indexOf('ROLE_VERIFY') != -1 || res.data.roles.indexOf('ROLE_SUPER_ADMIN') != -1){
									this.canVerify = true
									console.log('可以核销')
								}else{
									uni.showModal({
										title: '',
										content: '您没有核销权限,请联系门店管理员开通！',
										showCancel: false,
										success: () => {
											uni.switchTab({
												url: '/pages/index/index'
											})
										}
									})
								}
							})
						}
					}
				})
			} else {
				uni.showModal({
					title: '',
					content: '参数错误！',
					showCancel: false,
					success: () => {
						uni.switchTab({
							url: '/pages/orders/index'
						})
					}
				})
			}
		},

		methods: {
			verify(){
				uni.showModal({
					title: '',
					content: '一经核销，订单不能退回，是否确认核销该订单？',
					success: (data) => {
						if(data.confirm){
							this.request.post(`api/orders/${this.item.no}/verify`).then((res) => {
								this.item.status = 5
								uni.showModal({
									title: '',
									content: '核销成功！',
									showCancel: false,
									success: () => {
										
									}
								})
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		background-color: #f3f3f3;
		min-height: 100vh;
	}

	.group {
		margin: 10px;
		background-color: #fff;
		border-radius: 10px;
		padding: 10px;
	}

	.title {
		color: #909399;
		font-size: 14px;
		line-height: 32px;
	}

	.name {
		font-size: 14px;
		float: right;
		padding-right: 10px;
		line-height: 32px;
		color: #303133;
	}

	.date-btn {
		color: #f3f3f3;
		margin: 5px;
		background-color: #434152;
		border-radius: 5px;
		padding: 10px;
		text-align: center;
	}
</style>